#pragma once

#include "Log.hpp"
#include <unordered_map>
#include <mutex>
#include <websocketpp/server.hpp>
#include <websocketpp/config/asio_no_tls.hpp>

class Online_User_Manage
{
    typedef websocketpp::server<websocketpp::config::asio>::connection_ptr cp;
public:
    void enter_game_hall(int uid, cp& conn)
    {
        std::unique_lock<std::mutex> lock(_mtx);

        _hall_user.insert(std::make_pair(uid, conn));
    }
    void enter_game_room(int uid, cp& conn)
    {
        std::unique_lock<std::mutex> lock(_mtx);
        _room_user.insert(std::make_pair(uid, conn));

    }
    void exit_game_hall(int uid)
    {
        if(!is_in_game_hall(uid)) return;
        std::unique_lock<std::mutex> lock(_mtx);

        _hall_user.erase(uid);
    }
    void exit_game_room(int uid)
    {
        if(!is_in_game_room(uid)) return;
        std::unique_lock<std::mutex> lock(_mtx);

        _room_user.erase(uid);
    }

    bool is_in_game_hall(int uid)
    {
        std::unique_lock<std::mutex> lock(_mtx);
        return _hall_user.count(uid);
    }
    bool is_in_game_room(int uid)
    {
        std::unique_lock<std::mutex> lock(_mtx);
        return _room_user.count(uid);

    }

    cp get_connect_from_hall(int uid)
    {
        std::unique_lock<std::mutex> lock(_mtx);
        auto it = _hall_user.find(uid);
        if(it == _hall_user.end())
            return cp();

        return _hall_user[uid];
    }
    cp get_connect_from_room(int uid)
    {
        std::unique_lock<std::mutex> lock(_mtx);
        auto it = _room_user.find(uid);
        if(it == _room_user.end())
            return cp();
            
        return _room_user[uid];
    }
private:
    std::mutex _mtx; // 锁
    std::unordered_map<int, cp> _hall_user; // 大厅用户
    std::unordered_map<int, cp> _room_user; // 房间用户
};